package com.wskj.ws.config;


import com.wskj.ws.handler.MyWebSocketHandler;
import com.wskj.ws.interceptor.MyWebSocketInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * 注册websocket
 */
@Configuration
@EnableWebSocket
public class SpringWebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        System.out.println("注册websocket");
        //固定连接路径则使用/ws，如/ws后还有参数，则追加/*通配符
        registry.addHandler(new MyWebSocketHandler(), "/ws/*")//设置连接路径和处理器
                .setAllowedOrigins("*") //允许跨域访问
                .addInterceptors(new MyWebSocketInterceptor());//设置拦截器
    }
}